<!--
  - ControllerTotalEnergy.php
  - v1.3
  - 4/02/2011
  -->

<!--
  - Code Specifications:
  - This code processes the inputs of the user in ViewTotalEnergy.php
  - This also stores the inputs, mean, and standard deviation into the 
  - database table.
  -->
	
<?php
	//Gets required libraries
	require '../libraries/EditAnalysisLib.php';
	require '../libraries/ComputeAnalysisLib.php';
	require '../libraries/UserLibrary.php';
	
	//Formally starts session
	session_start();
	
	//Ensures that the analyst is logged in.
	if(isSessionSet("analyst", NULL, "../Logout.php"));
	
	//Gathers data input from ViewTotalEnergy.php
	$percentCProtein1 =  $_POST['22/1'];
	$percentCFat1 = $_POST['23/1'];
	$percentCarbs1 = $_POST['24/1'];	
	$percentCProtein2 =  $_POST['22/2'];
	$percentCFat2 = $_POST['23/2'];
	$percentCarbs2 = $_POST['24/2'];
	
	//Create connection to the database
	$connect = mysql_connect("localhost", "editanalysis", "editanalysis");	
	if (!$connect) {
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("casl", $connect);
	
	//Execute query
	$val = mysql_query("SELECT * FROM analysis_result WHERE casl_year =
			$_SESSION[year]	AND casl_sample = $_SESSION[no]");
	$row = mysql_fetch_array($val);	
				
	//Computes the total energy content from the first column of inputs
	if (isset($_POST['compute1'])) {
		$compute1 = ComputeTotalEnergy($percentCProtein1, $percentCFat1, 
				$percentCarbs1);
		$mean = ComputeMean($compute1, $row['result_value2'], 0, 2);
		$standardDeviation = ComputeStandardDeviation($compute1,
				$row['result_value2'], $mean, 2);
		$edit = editTotalEnergy($standardDeviation, $mean, "result_value1",
				$compute1, "value1", $percentCProtein1, $percentCFat1,
				$percentCarbs1, $_SESSION["year"], $_SESSION["no"], 22, 23,
				24, "casl", "localhost", "editanalysis", "editanalysis");
	//Computes the total energy content from the second column of inputs
	} else if (isset($_POST['compute2'])) {
		$compute2 = ComputeTotalEnergy($percentCProtein2, $percentCFat2, 
				$percentCarbs2);
		$mean = ComputeMean($compute2, $row['result_value1'], 0, 2);
		$standardDeviation = ComputeStandardDeviation($compute2,
				$row['result_value1'], $mean, 2);
		$edit = editTotalEnergy($standardDeviation, $mean, "result_value2",
				$compute2, "value2", $percentCProtein2, $percentCFat2,
				$percentCarbs2, $_SESSION["year"], $_SESSION["no"], 22, 23,
				24, "casl",	"localhost", "editanalysis", "editanalysis");
	//Computes the moisture content from both columns of inputs
	} else if (isset($_POST['computeBoth'])) {
		$compute1 = ComputeTotalEnergy($percentCProtein1, $percentCFat1, 
				$percentCarbs1);
		$compute2 = ComputeTotalEnergy($percentCProtein2, $percentCFat2, 
				$percentCarbs2);
		$mean = ComputeMean($compute1, $compute2, 0, 2);
		$standardDeviation = ComputeStandardDeviation($compute1, $compute2,
				$mean, 2);
		$edit1 = editTotalEnergy($standardDeviation, $mean, "result_value1",
				$compute1, "value1", $percentCProtein1, $percentCFat1,
				$percentCarbs1,	$_SESSION["year"], $_SESSION["no"], 22, 23,
				24, "casl", "localhost", "editanalysis", "editanalysis");
		$edit2 = editTotalEnergy($standardDeviation, $mean, "result_value2",
				$compute2, "value2", $percentCProtein2, $percentCFat2,
				$percentCarbs2, $_SESSION["year"], $_SESSION["no"], 22, 23,
				24, "casl", "localhost", "editanalysis", "editanalysis");
	}
	
	//Redirect back to ViewTotalEnergy.php
	header("Location: ViewTotalEnergy.php");
?>